package org.dromara.Nyt7FarmerInsuranceInfo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.Nyt7FarmerInsuranceInfo2;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.Nyt7FarmerInsuranceInfoAll;
import org.dromara.Nyt7FarmerInsuranceInfo.mapper.Nyt7FarmerInsuranceInfoMapper2;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.bo.Nyt7FarmerInsuranceInfoBo;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.vo.Nyt7FarmerInsuranceInfoVo;
import org.dromara.Nyt7FarmerInsuranceInfo.domain.Nyt7FarmerInsuranceInfo;
import org.dromara.Nyt7FarmerInsuranceInfo.mapper.Nyt7FarmerInsuranceInfoMapper;
import org.dromara.Nyt7FarmerInsuranceInfo.service.INyt7FarmerInsuranceInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 农户投保信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-27
 */
@RequiredArgsConstructor
@Service
public class Nyt7FarmerInsuranceInfoServiceImpl implements INyt7FarmerInsuranceInfoService {

    private final Nyt7FarmerInsuranceInfoMapper baseMapper;
    private final Nyt7FarmerInsuranceInfoMapper2 baseMapper2;
    private final SqlSessionFactory sqlSessionFactory;

    /**
     * 查询农户投保信息
     *
     * @param id 主键
     * @return 农户投保信息
     */
    @Override
    public Nyt7FarmerInsuranceInfoAll queryById(Long id) {
        Nyt7FarmerInsuranceInfoAll n = new Nyt7FarmerInsuranceInfoAll();
        Nyt7FarmerInsuranceInfo n1 = baseMapper.selectById(id);
        Nyt7FarmerInsuranceInfo2 n2 = baseMapper2.selectByFarmerId(n1.getId());
        BeanUtil.copyProperties(n1, n);
        BeanUtil.copyProperties(n2, n);
        return n;
    }

    /**
     * 分页查询农户投保信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 农户投保信息分页列表
     */
    @Override
    public TableDataInfo<Nyt7FarmerInsuranceInfoVo> queryPageList(Nyt7FarmerInsuranceInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Nyt7FarmerInsuranceInfo> lqw = buildQueryWrapper(bo);
        Page<Nyt7FarmerInsuranceInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的农户投保信息列表
     *
     * @param bo 查询条件
     * @return 农户投保信息列表
     */
    @Override
    public List<Nyt7FarmerInsuranceInfoVo> queryList(Nyt7FarmerInsuranceInfoBo bo) {
        LambdaQueryWrapper<Nyt7FarmerInsuranceInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }


    private LambdaQueryWrapper<Nyt7FarmerInsuranceInfo> buildQueryWrapper(Nyt7FarmerInsuranceInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7FarmerInsuranceInfo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFarmerName()), Nyt7FarmerInsuranceInfo::getFarmerName, bo.getFarmerName());
        lqw.like(StringUtils.isNotBlank(bo.getFarmerIdCard()), Nyt7FarmerInsuranceInfo::getFarmerIdCard, bo.getFarmerIdCard());
        lqw.eq(bo.getPlotCount() != null, Nyt7FarmerInsuranceInfo::getPlotCount, bo.getPlotCount());
        lqw.eq(bo.getMeasuredArea() != null, Nyt7FarmerInsuranceInfo::getMeasuredArea, bo.getMeasuredArea());
        lqw.eq(bo.getInsuredArea() != null, Nyt7FarmerInsuranceInfo::getInsuredArea, bo.getInsuredArea());
        lqw.like(StringUtils.isNotBlank(bo.getFamilyMemberName()), Nyt7FarmerInsuranceInfo::getFamilyMemberName, bo.getFamilyMemberName());
        lqw.like(StringUtils.isNotBlank(bo.getFamilyMemberIdCard()), Nyt7FarmerInsuranceInfo::getFamilyMemberIdCard, bo.getFamilyMemberIdCard());
        return lqw;
    }

    /**
     * 新增农户投保信息
     *
     * @param bo 农户投保信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7FarmerInsuranceInfoBo bo) {
        Nyt7FarmerInsuranceInfo add = MapstructUtils.convert(bo, Nyt7FarmerInsuranceInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改农户投保信息
     *
     * @param bo 农户投保信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7FarmerInsuranceInfoBo bo) {
        Nyt7FarmerInsuranceInfo update = MapstructUtils.convert(bo, Nyt7FarmerInsuranceInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7FarmerInsuranceInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除农户投保信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    /**
     * 获取农户数
     *
     * @return 农户数
     */
    @Override
    public Long getFarmerCount() {
        return baseMapper.selectCount(null);
    }

    @Override
    public boolean saveBatch(List<Nyt7FarmerInsuranceInfoAll> list) {
        for (Nyt7FarmerInsuranceInfoAll l : list) {
            Nyt7FarmerInsuranceInfo f1 = BeanUtil.copyProperties(l, Nyt7FarmerInsuranceInfo.class);
            baseMapper.insertOne(f1);
            Nyt7FarmerInsuranceInfo2 f2 = BeanUtil.copyProperties(l, Nyt7FarmerInsuranceInfo2.class);
            f2.setFarmerId(f1.getId());
            baseMapper2.insert(f2);
        }


        return true;
    }


    /**
     * 根据ID列表查询农户投保信息列表
     *
     * @param ids ID列表
     * @return 农户投保信息列表
     */
    @Override
    public List<Nyt7FarmerInsuranceInfoVo> queryListByIds(List<Long> ids) {
        LambdaQueryWrapper<Nyt7FarmerInsuranceInfo> lqw = Wrappers.lambdaQuery();
        lqw.in(Nyt7FarmerInsuranceInfo::getId, ids);
        return baseMapper.selectVoList(lqw);
    }
}
